<?php 
	Class Customerpackage extends CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->model('model_package');
		}	
		
		public function index(){
			$provide='';
			if(isset($_GET['provide'])) $provide=$_GET['provide'];
			if($provide=="0") {$pro='';}
			else $pro=' and destination like "%'.$provide.'%"';
			$title='';
			if(isset($_GET['title'])) $title=" and title like '%". $_GET['title']."%'";
			
			$data['data']=$this->model_package->getallbyprovide_name($pro,$title);		
			
			$data['template']='customerpackage/manage';
			$this->load->view('layout',$data);	
		}
		
		public function edit(){
			if(isset($_GET['id'])){
				$data['detail']	=$this->model_package->getdata(intval($_GET['id']));				
			}else $data['detail']=NULL;
			
				
			$data['template']='package/package_edit';
			$this->load->view('layout',$data);		
		}
		
		public function insert(){
			$data['title']=$_POST['title'];			
			$data['description']=$_POST['description'];
			$data['numberday']=$_POST['numberday'];
			$data['category']=$_POST['category'];			
			$data['destination']=$_POST['destination'];
			if($_FILES['fuavatar']['name'] != NULL){
                $config['upload_path'] = './uploads/package';
                $config['allowed_types'] = '*';
                $config['max_size']	= '3000';
                $config['max_width']  = '2704';
                $config['max_height']  = '2700';
                $this->load->library('upload',$config);
                if(!$this->upload->do_upload("fuavatar")){
                    header('Location: '.base_url().'/admin/package/index');
                    return FALSE;
                }else{
                    $dataimg = $this->upload->data();
                    $data['avatar'] = $dataimg['file_name'];
                }
            }			
			$data['promotion']=$_POST['promotion'];
			$data['overview']=$_POST['overview'];
			$data['itinerary']=$_POST['itinerary'];
			$data['include']=$_POST['include'];
			$data['exclude']=$_POST['exclude'];
			$data['policy']=$_POST['policy'];
			$data['guestreview']=$_POST['guestreview'];
			$data['status']=$_POST['status'];
			$data['length']=$_POST['length'];
			$data['recommendations']=$_POST['recommendations'];
			$this->model_package->add($data);
			header('Location: '.base_url().'/admin/package/index');
		}
		
		public function update(){
			if(isset($_GET["id"])){
				$data['title']=$_POST['title'];			
				$data['description']=$_POST['description'];
				$data['numberday']=$_POST['numberday'];
				$data['category']=$_POST['category'];			
				$data['destination']=$_POST['destination'];
				
				if($_FILES['fuavatar']['name'] != NULL){
	                $config['upload_path'] = './uploads/package';
	                $config['allowed_types'] = '*';
	                $config['max_size']	= '3000';
	                $config['max_width']  = '2704';
	                $config['max_height']  = '2700';
	                $this->load->library('upload',$config);
	                if(!$this->upload->do_upload("fuavatar")){
	                    header('Location: '.base_url().'/admin/package/index');
	                    return FALSE;
	                }else{
	                    $dataimg = $this->upload->data();
	                    $data['avatar'] = $dataimg['file_name'];
	                }
	            }			
				$data['promotion']=$_POST['promotion'];
				$data['overview']=$_POST['overview'];
				$data['itinerary']=$_POST['itinerary'];
				$data['include']=$_POST['include'];
				$data['exclude']=$_POST['exclude'];
				$data['policy']=$_POST['policy'];
				$data['guestreview']=$_POST['guestreview'];
				$data['status']=$_POST['status'];
				$data['length']=$_POST['length'];
				$data['recommendations']=$_POST['recommendations'];
				
				$this->model_package->update($data,intval($_GET['id']));
				header('Location: '.base_url().'/admin/package/index');
			}	
		}
		
		public function delete(){
			if(isset($_GET["id"])){
                $data = $this->model_package->getdata($_GET['id']);
                @unlink("uploads/package/".$data['avatar']);
				$this->model_package->del(intval($_GET['id']));	
				header('Location: '.base_url().'/admin/package/index');
			}	
		}
		
		public function changestatus(){			
			if(isset($_GET["id"])){		
				$data=$this->model_package->getdata(intval($_GET["id"]));								
				if(count($data)>0){
					if(isset($_GET["status"])){
						$data['status']=intval($_GET['status']);
						$this->model_package->update($data,intval($_GET["id"]));
						header('Location: '.base_url().'/admin/package/index');
					}
				}
			}
		}
        public function createThumbnail($fileName,$path,$width,$height){
            $this->load->library('image_lib');
            $config['image_library'] = 'gd2';
            $config['source_image'] = 'uploads/'.$path.'/'.$fileName;
            $config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['thumb_marker'] = FALSE;
            $config['width'] = $width;
            $config['height'] = $height;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
        }
	}
?>